﻿
function onDrawCell(e) {
    var column = e.column,
        field = e.field,
        value = e.value,
        node = e.record;
    if (field == "task_name_") {
        e.cellHtml = "<a onmouseleave=\"CloseTip()\" onmouseover=\"ShowTips(this,'" + node.task_id_ + "','" + node.task_name_ + "','" + node.description_ + "','" + node.assign_user_name_ + "','" + node.manage_user_name_ + "','" + node.status_ + "','" + node.broken_ + "','" + node.estimated_end_time_ + "')\" onclick=\"EditTask('" + node.task_id_ + "')\" href=\"javascript:void(0);\" style=\"color:blue;text-decoration:none;\">" + GetDesignation(value, 16) + "</a>"; //GetDelay(node.estimated_end_time_, node.status_)
        //<img src=\"/Static/img/itask/add_icon.png\" onclick=\"ResoleTask()\" />
    }
    if (field == "description_") {
        e.cellHtml = GetDesignation(value, 16);
    }
    if (field == "manage_user_name_") {
        if (node.assist_user_name_ != null && node.assist_user_name_ != "" && node.assist_user_id_ != null && node.assist_user_id_ != undefined) {
            e.cellHtml = value + "==>" + node.assist_user_name_;
        } else {
            e.cellHtml = value;
        }
    }
    if (field == "status_") {
        //e.cellHtml = FormatTaskStatus(node.status_);
        var fmt = GetDelay(node.estimated_end_time_, node.status_);
        e.cellHtml = fmt == '' ? "进行中" : "<font color=\"red\">" + fmt + "</font>";
    }
    if (field == "target_description_" || field == "work_report_" || field == "nextTarget" ||
        field == "lastReportOfMonth") {
        var desValue = "";
        try {
            if (value != null) {
                desValue = value.replace(/\r\n|\n|\r/g, '<br/>');
            }
        } catch (e) {

        }
        e.cellHtml = "<a onmouseleave=\"CloseTip()\" onmouseover=\"ShowSigTips(this,'" + desValue + "')\"> " + GetDesignation(value, 16) + "</a>";

    }
}

function EditTask(taskID) {
    window.open("/iTask/MyTask/TaskEdit.aspx?task_id_=" + taskID, "_blank");
    //window.top.location.href = "/iTask/MyTask/TaskEdit.aspx?task_id_=" + taskID;
}
function GetDesignation(value, length) {
    if (value == null) return "";
    value = String(value);
    return value.length > length ? value.substr(0, length) + "..." : value;
}

function CheckIsNewTask(manageNewFlag, assistNewFlag, manageUserId, assistUserId, userID) {
    if ((manageNewFlag != null && manageNewFlag == 0 && userID != null && manageUserId == userID) || (assistNewFlag != null && assistNewFlag == 0 && userID != null && assistUserId == userID)) {
        return true;
    } else {
        return false;
    }
}

function ShowTips(obj, taskId, taskName, description, assignUserName, manageUserName, status, broken, endDate) {

    $.ajax({
        url: "/AJAX/AjaxService.aspx?method=GetMemo",
        type: "post",
        data:
        {
            task_id_: taskId
        },
        cache: false,
        success: function (text) {
            var o = mini.decode(text);
            var data = "<div class=\"tips\"><ul>";
            data += "<li><span>任务名称:</span>" + taskName + "</li>";
            data += "<li><span>任务目标:</span>" + GetDesignation(description, 50) + "</li>";
            data += "<li><span>下达人:</span>" + assignUserName + "</li>";
            data += "<li><span>负责人:</span>" + manageUserName + "</li>";
            data += "<li><span>任务属性:</span>" + o.property + "</li>";
            data += "<li><span>最后汇报内容:</span>" + GetDesignation(o.report, 50) + "</li>";
            data += "</ul></div>";
            var box = document.getElementById("d_tip");
            box.innerHTML = data;
            ShowBox(obj, box);
        }
    });
}


function ShowSigTips(obj, value) {

    var data = "<div class=\"sigtips\"><ul>";

    data += "<li>" + value + "</li></ul></div>";
    var box = document.getElementById("d_tip");
    box.innerHTML = data;
    ShowBox(obj, box);
}

function ShowBox(obj, box) {

    var h = 0; //(boxName == "UserBox") ? 15 : 25;
    var left = getLeft(obj);
    box.style.display = "block";
    var boxHeight = box.clientHeight;

    obj.onmousemove = function (e) {
        var e = e || window.event;
        var mouse = { x: e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft, y: e.clientY + document.body.scrollTop + document.documentElement.scrollTop };

        if (mouse.y > document.body.clientHeight) {
            mouse.y = document.body.clientHeight - boxHeight;
        }
        else {
            if (mouse.y > document.body.clientHeight / 2 && mouse.y > document.body.clientHeight - boxHeight - h) {

                mouse.y = mouse.y - boxHeight - h;
            }
        }

        if (mouse.x + box.offsetWidth > document.body.offsetWidth) {
            mouse.x = mouse.x - box.offsetWidth;
        }
        //        else {
        //            mouse.x = mouse.x - obj.offsetWidth;
        //        }
        box.style.left = mouse.x + 20 + 'px';
        box.style.top = mouse.y + 10 + 'px';
    }

    //    var top = getTop(obj);
    //    if (top > document.body.clientHeight) {
    //        top = document.body.clientHeight - box.clientHeight;
    //    }
    //    else {
    //        if (top > 250 && top > document.body.clientHeight - box.clientHeight - h) {
    //            top = top - box.clientHeight - h;
    //        }
    //    }

    //    box.style.top = (top + h) + "px";

    //    if (left + box.offsetWidth > document.body.offsetWidth) {
    //        box.style.left = (left - box.offsetWidth) + "px";
    //    }
    //    else {
    //        box.style.left = (left + obj.clientWidth) + "px";
    //    }
}

function getLeft(e) {
    var t = e.offsetLeft;
    while (e = e.offsetParent) {
        t += e.offsetLeft;
    }
    return t;
}
function getTop(e) {
    //alert(e.offsetTop);
    var t = e.offsetTop + parent.document.body.scrollTop;
    while (e = e.offsetParent) {
        t += e.offsetTop;
    }

    return t;
}
function CloseTip() {
    setTimeout(new function () { document.getElementById("d_tip").style.display = "none"; }, 300);
}
function FormatTaskStatus(value) {
    var dataResult = [{ "id": "0", "text": "新建" }, { "id": "1", "text": "进行中" }, { "id": "2", "text": "已完成" }, { "id": "3", "text": "已暂停" }, { "id": "5", "text": "已终止"}];
    //  var dataResult = text;
    for (var i = 0, l = dataResult.length; i < l; i++) {
        var g = dataResult[i];
        if (g.id == value) return g.text;
    }
    return "";
}
function FormatBroken(value) {
    var dataResult = [{ "id": "0", "text": "未分解" }, { "id": "1", "text": "已分解"}];
    //  var dataResult = text;
    for (var i = 0, l = dataResult.length; i < l; i++) {
        var g = dataResult[i];
        if (g.id == value) return g.text;
    }
    return "";
}

function GetDelay(value, status) {
    if (status == 0 || status == 1) {
        var currentDate = new Date().Format("yyyy-MM-dd");
        var endDate = new Date(value).Format("yyyy-MM-dd");
        //alert(endDate);
        var a1 = currentDate.split("-");
        var b1 = endDate.split("-");

        var d1 = new Date(a1[0], a1[1], a1[2]); //当前时间
        var d2 = new Date(b1[0], b1[1], b1[2]); //任务计划完成时间

        if (Date.parse(d1) - Date.parse(d2) <= 0) {
            return '';
        }
        return "[延期" + DateDiff(currentDate, endDate) + "天]";
    } else if (status == 2) {
        return "已完成";
    } else {
        return '';
    }
}

function DateDiff(sDate1, sDate2) {  //sDate1和sDate2是yyyy-MM-dd格式
    var aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);  //转换为yyyy-MM-dd格式
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
    return iDays;  //返回相差天数
}

function ResoleTask() {
    var row = tree.getSelected();
    if (row) {
        mini.open({
            url: "/iTask/MyTask/TaskResole.aspx",
            title: "任务分解", width: 700, height: 350,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { parent_task_id_: row.task_id_, estimated_start_time_: row.estimated_start_time_, estimated_end_time_: row.estimated_end_time_ };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                tree.reload();
            }
        });
    } else {
        alert("请选中一条记录");
    }
}
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
Date.prototype.addDays = Date.prototype.addDays || function (days) {
    this.setDate(this.getDate() + days);
    return this;
};
Date.prototype.addMonth = Date.prototype.addMonth || function (month) {
    var tempDate = this.getDate();
    this.setMonth(this.getMonth() + month);
    if (tempDate != this.getDate()) this.setDate(0);
    return this;
};

function unfoldSearch() {
    var div = document.getElementById("div_SearchPanel");
    var txt_key = document.getElementById("txt_key");
    var a = mini.get("a_search");
    if (a.getText() == "高级查询" || a.getText() == "展开查询条件") {
        div.style.display = '';
        txt_key.style.display = 'none';
        a.setText("收缩查询条件");
    }
    else if (a.getText() == "收缩查询条件") {
        div.style.display = 'none';
        txt_key.style.display = '';
        a.setText("展开查询条件");
    }
}